<template>
  <!-- 表格卡片 -->
  <a-card size="small" :bordered="false" :hoverable="true" :scroll="{ x: 1500 }" title="捐赠明细列表">
    <template #extra>
      <a-tooltip placement="top">
        <template #title>
          <span>返回上级</span>
        </template>
        <a-button size="small" @click="getAddInstantly(0)"><RollbackOutlined /></a-button>
      </a-tooltip>
    </template>

    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-row :gutter="16">
          <a-col :span="6"> <a-button type="primary" @click="getImport"> 导出 </a-button></a-col>
        </a-row>
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.OA.ENTERPRISE" :refresh="getInitDate" />
      </div>
    </a-row>
    <a-table
      size="small"
      :dataSource="tableData"
      :columns="columns"
      :row-key="(record) => record.blfinancprojectid"
      :pagination="false"
      :loading="tableLoading"
      bordered
      @change="handleTableChange"
    >
    </a-table>
    <!-- 分页-->
    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.limit"
        v-model:current="queryForm.page"
        v-model:pageSize="queryForm.limit"
        :total="total"
        @change="getInitDate"
        @showSizeChange="getInitDate"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>
</template>
<script setup>
  import { reactive, ref, onMounted, defineEmits, defineProps } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { Funding } from '/@/api/funding/funding-api';
  import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import TableOperator from '/@/components/support/table-operator/index.vue';
  import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';
  import { smartSentry } from '/@/lib/smart-sentry';
  //--------------------------- 表格 列 ---------------------------
  const columns = ref([
    {
      title: '捐赠人',
      dataIndex: 'blname',
      align: 'center',
      width: 150,
    },
    {
      title: '捐赠金额',
      dataIndex: 'blamount',
      align: 'center',
    },
    {
      title: '捐赠时间',
      dataIndex: 'bldate',
      align: 'center',
      sorter: (a, b) => {
        let aTimeString = a.bldate;
        let bTimeString = b.bldate;
        aTimeString = aTimeString.replace(/-/g, '/');
        bTimeString = bTimeString.replace(/-/g, '/');
        let aTime = new Date(aTimeString).getTime();
        let bTime = new Date(bTimeString).getTime();
        return bTime - aTime;
      },
    },
  ]);
  const queryFormState = {
    page: 1,
    limit: PAGE_SIZE,
  };
  const queryForm = reactive({ ...queryFormState });
  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);
  //拿到初始化信息
  const props = defineProps({
    DataId: String,
  });
  async function getInitDate() {
    try {
      queryForm.key = props.DataId;
      let res = await Funding.FindDonation(queryForm);
      tableData.value = res.records;
      total.value = res.total;
      console.log(props.DataId);
    } catch (error) {
      smartSentry.captureError(error);
    }
  }
  //   导出
  async function getImport() {
    try {
      let obj = {
        projectNum: props.DataId,
      };
      let res = await Funding.exportImport(obj);
      const link = document.createElement('a');
      const url = window.URL.createObjectURL(new Blob([res.data]));
      link.href = url;
      link.download = '捐赠人明细.xlsx';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      window.URL.revokeObjectURL(url);
      message.success(`下载成功！`);
    } catch (error) {
      smartSentry.captureError(error);
    }
  }
  //返回立项申请表格页面
  const emit = defineEmits();
  const getAddInstantly = (e) => {
    let obj = {
      type: e,
    };
    emit('getBlack', obj);
  };

  onMounted(() => {
    getInitDate();
  });
</script>
